package com.nd.teamfile.fileexplorer.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.nd.teamfile.fileexplorer.R;
import com.nd.teamfile.fileexplorer.adapter.FMLocalFileAdapter;
import com.nd.teamfile.fileexplorer.fragment.FileExplorerFragment;
import com.nd.teamfile.fileexplorer.sdk.type.FileInfo;
import com.product.android.ui.activity.HeaderActivity;
import com.product.android.utils.FileHelper;

public class FMLocalFileActivity extends HeaderActivity {
	private final int SECOND_REQUEST_CODE = 2;

	//接收需要展示的路径信息
	private HashMap<Integer, String> mPathDirs;
	
	// 展示列表信息
	private ArrayList<FileInfo> mInfoList = new ArrayList<FileInfo>();

	// 有几种路径
	public static final String PATH_DIRS = "PATH_DIRS";

	@Override
	protected boolean _onCreate(Bundle savedInstanceState) {
		if (super._onCreate(savedInstanceState)) {
			this.setContentView(R.layout.fm_localfile);
			getIntentData();
			initComponent();
			setActivityTitle(getString(R.string.file_local_file));
			super.initEvent();
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	private void getIntentData() {
		Intent intent = getIntent();
		if (intent.getExtras() != null) {
			mPathDirs = (HashMap<Integer, String>) intent
					.getSerializableExtra(PATH_DIRS);
		}
		if (mPathDirs == null || mPathDirs.size() == 0) {
			Toast.makeText(this, R.string.get_file_failure, Toast.LENGTH_LONG)
					.show();
			finish();
		} else {
			// 如果获取到的路径信息不为空，则判断对应的类型。
			// 内存路径和SD卡路径每次设置完就从Map中移除，剩下的认为是扩展存储路径信息用for循环遍历
			if (mPathDirs.containsKey(FileHelper.TYPE_PHONE_MEMORY)) {
				// 内存路径
				FileInfo fiPhoneMemory = new FileInfo();
				fiPhoneMemory.setFilepath(mPathDirs
						.get(FileHelper.TYPE_PHONE_MEMORY));
				fiPhoneMemory.setName(getString(R.string.file_phone_memory));
				fiPhoneMemory.setState(R.drawable.file_phone_memory);
				mInfoList.add(fiPhoneMemory);
				mPathDirs.remove(FileHelper.TYPE_PHONE_MEMORY);
				if (mPathDirs.size() == 0)
					return;
			}
			if (mPathDirs.containsKey(FileHelper.TYPE_SD_MEMORY)) {
				// SD卡路径
				FileInfo fiPhoneMemory = new FileInfo();
				fiPhoneMemory.setFilepath(mPathDirs
						.get(FileHelper.TYPE_SD_MEMORY));
				fiPhoneMemory.setName(getString(R.string.file_sd_memory));
				fiPhoneMemory.setState(R.drawable.file_sd_memory);
				mInfoList.add(fiPhoneMemory);
				mPathDirs.remove(FileHelper.TYPE_SD_MEMORY);
				if (mPathDirs.size() == 0)
					return;
			}

			// 剩下的为扩展存储路径
			Object[] exsdDirs =  mPathDirs.values().toArray();
			int exsdNum = exsdDirs.length;
			for (int i = 0; i < exsdNum; i++) {
				String dir = (String)exsdDirs[i];
				FileInfo fiExsdMemory = new FileInfo();
				fiExsdMemory.setFilepath(dir);
				fiExsdMemory.setName(String.format(
						getString(R.string.file_exsd_memory), (i + 1)));
				fiExsdMemory.setState(R.drawable.file_exsd_memory);
				mInfoList.add(fiExsdMemory);
			}
		}
	}

	@Override
	protected void initComponent() {
		super.initComponent();
		ListView mListView = (ListView) findViewById(R.id.fm_localfile_lv);
		mListView.setCacheColorHint(0); // 去掉黑色背景
		mListView.setDivider(null); // 去掉每项下面的黑线(分割线)
		mListView.setAdapter(new FMLocalFileAdapter(this, mInfoList));
		mListView.setOnItemClickListener(listviewItemClickListener);
	}

	private OnItemClickListener listviewItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			FileInfo info = (FileInfo) parent.getAdapter().getItem(position);
			Intent intent = new Intent();
			intent.putExtra(FileExplorerFragment.CUR_PATH, info.getFilepath());
			intent.setClass(FMLocalFileActivity.this, FileExplorerActivity.class);
			startActivityForResult(intent, SECOND_REQUEST_CODE);
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == SECOND_REQUEST_CODE
				&& resultCode == Activity.RESULT_OK) {
			this.setResult(Activity.RESULT_FIRST_USER, data);
			this.finish();
		} else {
			if (mInfoList == null || mInfoList.size() == 1) {
				this.finish();
			}
		}
	}
}